package pulpfizz.pulp;

import pulpcore.Input;
import pulpcore.Stage;
import pulpcore.animation.Easing;
import pulpcore.animation.Fixed;
import pulpcore.sprite.Group;

public class Camera
{
	public Fixed x;
	public Fixed y;
	public Fixed zoom;
	public Fixed angle; 

	private Group canvasGroup;
	private double naturalWidth;
	private double naturalHeight;
	private boolean respondToInput;
	
	public Camera()
	{
		x = new Fixed(0.0);
		y = new Fixed(0.0);
		zoom = new Fixed(1.0);
		angle = new Fixed(0.0);
		
		respondToInput = true;
	}
	
	public void applyTransformations()
	{
//		double widthOfCanvasToShow = Stage.getWidth();
//		double heightOfCanvasToShow = Stage.getHeight();
		
		double offsetX = -x.get() * zoom.get() + Stage.getWidth()/2;
		double offsetY = -y.get() * zoom.get() + Stage.getHeight()/2;
		
		canvasGroup.x.set(offsetX);
		canvasGroup.y.set(offsetY);
		
		canvasGroup.width.set(naturalWidth * zoom.get());
		canvasGroup.height.set(naturalHeight * zoom.get());
		
		canvasGroup.angle.set(angle.get());
	}

	public void update(int elapsedTime)
	{
		x.update(elapsedTime);
		y.update(elapsedTime);
		zoom.update(elapsedTime);
		angle.update(elapsedTime);
		applyTransformations();
		
		handleInput();
	}
	
	public void zoomBy(double zoomFactor)
	{
		double origZoom = zoom.get();
		zoom.update(ZOOM_DURATION);
		double origDest = zoom.get();
		zoom.animate(origZoom,origDest*zoomFactor, ZOOM_DURATION,Easing.REGULAR_OUT);
	}
	
	public void zoomTo(double zoomLevel)
	{
		zoom.animateTo(zoomLevel, ZOOM_DURATION,Easing.REGULAR_OUT);
	}
	
	final static double ZOOM_FACTOR = 2.0f;
	final static int ZOOM_DURATION = 1000;
	protected void handleInput()
	{
		if (!respondToInput)
		{
			return;
		}
		if (Input.isTyped(Input.KEY_Q))
		{
			zoomBy(ZOOM_FACTOR);
		}
		if (Input.isTyped(Input.KEY_W))
		{
			zoomBy(1 / ZOOM_FACTOR);
		}
		if (Input.isTyped(Input.KEY_LEFT))
		{
			double dX = .25 * Stage.getWidth();
//			panBy(dX, 0);
		}
		if (Input.isTyped(Input.KEY_RIGHT))
		{
			double dX = -.25 * Stage.getWidth();
//			panBy(dX, 0);
		}
		if (Input.isTyped(Input.KEY_UP))
		{
			double dY = .25 * Stage.getHeight();
//			panBy(0, dY);
		}
		if (Input.isTyped(Input.KEY_DOWN))
		{
			double dY = -.25 * Stage.getHeight();
//			panBy(0, dY);
		}

		int mouseWheel = Input.getMouseWheelRotation();
		if (mouseWheel != 0)
		{
//			double mX = getLocalX(Input.getMouseX(),Input.getMouseY());
//			double mY = getLocalY(Input.getMouseX(),Input.getMouseY());
//			setAnchor(mX, mY);
			if (mouseWheel > 0)
				zoomBy(1 / ZOOM_FACTOR);
			else
				zoomBy(ZOOM_FACTOR);
		}
	}

	/**
	 * @return the canvasGroup
	 */
	public Group getCanvasGroup()
	{
		return canvasGroup;
	}

	/**
	 * @param canvasGroup the canvasGroup to set
	 */
	public void setCanvasGroup(Group canvasGroup)
	{
		this.canvasGroup = canvasGroup;
		naturalWidth = canvasGroup.width.get();
		naturalHeight = canvasGroup.height.get();
	}
	
}
